package cn.com.haopy.dataes.db.service.main.impl;

import cn.com.haopy.dataes.db.entity.main.HDPermission;
import cn.com.haopy.dataes.db.mapper.main.HDPermissionMapper;
import cn.com.haopy.dataes.db.service.main.HDPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class HDPermissionServiceImpl extends ServiceImpl<HDPermissionMapper, HDPermission> implements HDPermissionService {
    @Override
    public Set<String> queryByRoleIds(Integer[] roleIds) {
        Set<String> permissions = new HashSet<String>();
        if (roleIds.length == 0) {
            return permissions;
        }

        LambdaQueryWrapper<HDPermission> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(HDPermission::getRoleId, roleIds);
        List<HDPermission> permissionList = super.list(queryWrapper);

        for(HDPermission permission : permissionList){
            permissions.add(permission.getPermission());
        }

        return permissions;
    }

    @Override
    public Set<String> queryByRoleId(Integer roleId) {
        Set<String> permissions = new HashSet<String>();
        if(roleId == null){
            return permissions;
        }

        LambdaQueryWrapper<HDPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDPermission::getRoleId, roleId);
        List<HDPermission> permissionList = super.list(queryWrapper);

        for(HDPermission permission : permissionList){
            permissions.add(permission.getPermission());
        }

        return permissions;
    }

    @Override
    public boolean checkSuperPermission(Integer roleId) {
        if (roleId == null) {
            return false;
        }

        LambdaQueryWrapper<HDPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDPermission::getRoleId, roleId)
                .eq(HDPermission::getPermission, "*");

        return super.count(queryWrapper) != 0;
    }

    @Override
    public boolean deleteByRoleId(Integer roleId) {
        LambdaQueryWrapper<HDPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDPermission::getRoleId, roleId);
        return super.remove(queryWrapper);
    }
}
